package com.atguigu.gmall.product.controller;

import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product/baseTrademark")
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    @ApiOperation(value = "分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit) {
        Page<BaseTrademark> baseTrademarkPage = new Page<>(page, limit);

        IPage<BaseTrademark> baseTrademarkIPage = baseTrademarkService.getPage(baseTrademarkPage);
        return Result.ok(baseTrademarkIPage);
    }

    //  添加品牌数据：
    //  http://api.gmall.com/admin/product/baseTrademark/save
    //  前端传递的是Json 数据 ,需要将其转换为JavaObject
    @PostMapping("save")
    public Result save(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }

    //  删除数据
    //  http://localhost/admin/product/baseTrademark/remove/11
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id) {
        baseTrademarkService.removeById(id);
        return Result.ok();
    }


    //  回显数据
    //  http://api.gmall.com/admin/product/baseTrademark/get/{id}
    @GetMapping("get/{id}")
    public Result get(@PathVariable String id) {
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }

    //  修改的按钮
    //  http://api.gmall.com/admin/product/baseTrademark/update
    //  通过api 数据接口文档，得知前端传递的是Json 数据
    @PutMapping("update")
    public Result update(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }

    /**
     * 查询全部品牌
     *
     * @return
     */
    @GetMapping("getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> list = baseTrademarkService.list(null);
        return Result.ok(list);
    }


}
